<!--
 * Date: 2018年01月02日 09:32:33
 * LastEditors: lvyou
 * LastEditTime: 2019年11月29日 11:55:16
 * FilePath: \youlinweb-master\src\pages\replace\publicereplace\publishreplace.vue
 * copyright(c) 2019-2025 版权为浙江双城网络科技集团有限公司所有，违者必究。
 -->
<template >
  <div>
    <ylheader @save="savecaogao" @send='sendcomment' :lefttext="true" :lefttexts="'取消'" :title="'发布闲置'" :righttext="true" :righttexts="'发送'"></ylheader>
    <div class="loadmore-container" ref="searchWrapper">
      <div class="loadmore-ul">

        <div>
          <div class="replace-title">
            <input type="text" v-model="replacetitle" class="replace-title-input" placeholder="标题（必填）" />
          </div>
          <publishimgs :textareatexts="content" :thumbnailslist="pics" @haha="textareatext" @commentpics="uploadpics" @changeTextArea="changeTextArea"></publishimgs>
        </div>

        <!--设置价格地区事间等 -->
        <div class="publish-bottom">
          <div class="my-cell">
            <div style="display: flex;color: #333333;line-height:0.4rem">
              <span class="yl-2x-money leftmoneysvg" style="line-height:0.8rem"></span>
              <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';line-height:0.8rem">价格（元）</span>
              <input type="number" step="0.01" v-model="custommoney" @keydown="handleInput2" class="my-money-input" placeholder="0.00" :class="{nodata:showmoney}" @focus="scrollToBottom"
                ref="input" />
            </div>
            <div @click="choosemoney" class="is-link-svg" style="position:absolute;display:flex;line-height:0.7rem;right:1.3rem" :class="{gouselected:showmoney}">
              <div class="yl-2gou goutyle "></div>
              <div class="facedismoney">价格面议</div>
            </div>
            <div>
            </div>
          </div>

          <div class="my-cell">
            <div style="display: flex;color: #333333">
              <span class="yl-2x-map leftmoneysvg"></span>
              <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';">显示区域</span>
            </div>
            <div @click="chooseplace">
              <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';color: #999999">{{defaultplace}}</span>
              <div class="is-link-svg">
                <span class="yl-2list-r "></span>
              </div>
            </div>
          </div>

          <div class="my-cell">
            <div style="display: flex;color: #333333">
              <span class="yl-2x-time leftmoneysvg"></span>
              <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';">有效时间</span>
            </div>
            <div @click="choosetime">
              <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';color: #999999">{{defaulttime}}天</span>
              <div class="is-link-svg">
                <span class="yl-2list-r "></span>
              </div>
            </div>
          </div>

        </div>
      </div>

      <mt-popup v-model="showplace" position="bottom">
        <div style="width: 7.5rem">
          <div class="placediv" v-for="(item,index) in placelist" :key="index" @click="choosewhiceplace(index)">
            <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';color: #333333;">{{item.place}}</span>
            <div style="width: 6.9rem;height: 1px;background-color: #E6E6E6;margin-left: 0.3rem"></div>
          </div>
        </div>
      </mt-popup>

      <mt-popup v-model="showtime" position="bottom">
        <div style="width: 7.5rem">
          <div class="placediv" v-for="(item,index) in timelist" :key="index" @click="choosewhicetime(index)">
            <span style="font-size: 0.3rem;font-family: 'PingFangSC Regular';color: #333333;">{{item.time}}</span>
            <div style="width: 6.9rem;height: 1px;background-color: #E6E6E6;margin-left: 0.3rem"></div>
          </div>
        </div>
      </mt-popup>

    </div>
  </div>
</template>
<script>
import ylheader from 'src/components/yl-header.vue'
import publishimgs from 'src/components/public-replace-imgs.vue'
import { Cell } from 'mint-ui';
import api from 'src/api/index'
import { mapMutations } from 'vuex'
import { Indicator } from "mint-ui";
export default {
  name: "publishreplace",
  data () {
    return {
      defaultplace: '本小区',
      placelist: [{ place: '本小区' }, { place: '本城市' }, { place: '全国' }],
      showplace: false,
      defaulttime: '30',
      timelist: [{ time: '30' }, { time: '15' }, { time: '7' }, { time: '3' }],
      showtime: false,
      custommoney: '',
      moneylist: [{ money: '0.00' }],
      showmoney: false,
      replacetitle: '',
      content: '',
      pics: [],
      uploadpictures: [],
      picstring: [],
      caogaoId: '',
      showType: '1',
      park: '',
      city: '',
      replaceId: '',
      draft: '',
      is_r: false,
      replacedResult: '',
      isalreadysend: false,//是否已经点击发送
      iosbodyheight: "",//ios页面高度
      androidbodyheight: "",//android页面高度
    }
  },
  components: {
    ylheader, publishimgs, Cell
  },
  directives: {
    focus: {
      // 指令的定义
      inserted: function (el) {
        el.focus()
      }
    }
  },
  mounted () {
    this.initLookreplacedraft().then(data => {
      return this.initScroll();
    }).then(data => {
      this.iosbodyheight = $('body').scrollTop();
      this.androidbodyheight = $(window).height();
    });
  },
  methods: {
    ...mapMutations([
      'changereplacestatus'
    ]),
    //价格获取焦点的时候滚动到底部，防止页面被软键盘遮挡
    scrollToBottom () {
      setTimeout(() => {
        this.myscroll.refresh();
        this.myscroll.scrollTo(0, this.myscroll.maxScrollY, 0);
      }, 1000)
    },
    handleInput2 (e) {
      // 通过正则过滤小数点后两位
      e.target.value = (e.target.value.match(/^\d*(\.?\d{0,1})/g)[0]) || null
    },
    initLookreplacedraft () {
      let p = new Promise((resolve, reject) => {
        if (this.$route.query.isrepeat) {
          this.draft = 0
        } else {
          this.draft = 1
        }
        const data = {
          replacedId: this.$route.query.replacedId,
          draft: this.draft
        }
        /**
         * 查询闲置草稿
         */
        api.Querylookreplacedraft(data).then(res => {
          if (res.success) {
            if (res.data) {
              this.pics = res.data.picture
              this.content = res.data.content
              this.caogaoId = res.data.id
              this.replacetitle = res.data.replacedTitle
              this.custommoney = res.data.price
              this.showType = res.data.showType
              this.defaulttime = res.data.effectiveTime
            }
            if (this.showType == '1') {
              this.defaultplace = '本小区'
            } else if (this.showType == '2') {
              this.defaultplace = '本城市'
            } else if (this.showType == '3') {
              this.defaultplace = '全国'
            }
          }
          resolve("得到置换信息...");
        })
      });
      return p;
    },
    //初始化scroll
    initScroll () {
      let p = new Promise((resolve, reject) => {
        this.$nextTick(() => {
          let searchWrapper = this.$refs.searchWrapper;
          this.setScroll(searchWrapper, this);
        });
        resolve("初始化scroll结束...");
      });
      return p;
    },
    setScroll (searchWrapper, _this) {
      _this.myscroll = new iScroll(searchWrapper, {
        //上拉时触发
        onScrollMove: function () {
          if ((this.y <= (this.wrapperH - this.scroller.clientHeight - 50)) && _this.is_r == false) {
            //加载数据
            _this.is_r = true;
          }
        },
        onScrollEnd: function () {
        },
        onScrollStart: function () {
          this.refresh();
        }

      });
    },
    textareatext (data) {
      this.content = data
    },
    uploadpics (data) {
      this.uploadpictures = data
      this.$nextTick(() => {
        this.myscroll.refresh();
      });
    },
    savecaogao () {
      if (this.isalreadysend) {
        return;
      }
      this.isalreadysend = true
      this.uploadpictures = [...this.pics]
      const array = []
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src)
      })
      this.picstring = array.join(',')
      if (!this.content && !this.picstring && !this.replacetitle) {
        this.isalreadysend = false;
        this.$router.go(-1)
        return
      }
      const replaced = {
        id: this.caogaoId,
        replacedTitle: this.replacetitle,
        draft: 1,
        content: this.content,
        showType: this.showType,
        price: this.custommoney,
        effectiveTime: this.defaulttime,
        issuedPlace: localStorage.getItem("place"),
        pictureJson: this.picstring,
        areaName: localStorage.getItem("park"),
        city: localStorage.getItem("city"),
        park: localStorage.getItem("park")
      }
      api.Querypublishreplace(replaced).then(res => {
        if (res.success) {
          if (!this.$route.query.isrepeat) {
            this.$router.go(-1)
          } else {
            this.$router.go(-2)
          }
        } else {
          this.isalreadysend = false;
          this.$toast(res.msg)
        }
      }).catch(error => {
        this.isalreadysend = false;
      })
    },
    sendcomment () {
      if (this.isalreadysend) {
        return;
      }
      if (this.$route.query.isrepeat) {
        this.replacedResult = 0
      } else {
        this.replacedResult = null
      }

      var myshowtype = this.showType.toString()
      this.changereplacestatus(myshowtype)
      this.uploadpictures = [...this.pics]
      const array = []
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src)
      })
      this.picstring = array.join(',')
      if (!this.replacetitle) {
        this.$toast("请编辑标题");
        this.isalreadysend = false;
        return
      }
      if (!this.content && !this.picstring) {
        this.$toast("请编辑内容")
        this.isalreadysend = false;
        return
      }
      // this.replacetitle = this.$util.getTextUrl(this.replacetitle);//识别链接
      // this.content = this.$util.getTextUrl(this.content);//识别链接
      Indicator.open("loading...");
      const replaced = {
        id: this.caogaoId,
        replacedTitle: this.replacetitle,
        draft: 0,
        content: this.content,
        showType: this.showType,
        price: this.custommoney,
        effectiveTime: this.defaulttime,
        issuedPlace: localStorage.getItem("place"),
        pictureJson: this.picstring,
        areaName: localStorage.getItem("park"),
        city: localStorage.getItem("city"),
        park: localStorage.getItem("park"),
      }
      if (this.replacedResult) replaced.replacedResult = this.replacedResult
      this.isalreadysend = true
      api.Querypublishreplace(replaced).then(res => {

        if (res.success) {
          this.$util.saveFootPrint({ actType: 5, typeValue: res.value });//用户轨迹
          if (!this.$route.query.isrepeat) {
            if (this.$route.query.tag == 0) {
              this.$router.go(-1);
            } else if (this.$route.query.tag == 1) {
              this.$router.push({ path: "/replace" });
            }
          } else {
            this.$router.go(-2)
          }
        } else {
          Indicator.close();
          this.isalreadysend = false;
          this.$toast(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    },

    chooseplace () {
      this.showplace = true
    },
    choosewhiceplace (index) {
      this.defaultplace = this.placelist[index].place
      if (this.defaultplace == '本小区') {
        this.showType = '1'
      } else if (this.defaultplace == '本城市') {
        this.showType = '2'
      } else if (this.defaultplace == '全国') {
        this.showType = '3'
      }
      this.showplace = false
    },
    choosetime () {
      this.showtime = true
    },
    choosewhicetime (index) {
      this.defaulttime = this.timelist[index].time
      this.showtime = false
    },
    choosemoney () {
      this.showmoney = !this.showmoney
      if (this.showmoney == true) {
        this.custommoney = "0.00";
      } else if (this.showmoney == false) {
        this.custommoney = ""
      }
    },
    choosewhicemoney (index) {
      this.custommoney = this.moneylist[index].money
      this.showmoney = false
    },
    changeTextArea () {
      this.$nextTick(() => {
        this.myscroll.refresh();
      });
    }

  }
}
</script>
<style lang="less" scoped>
.replace-title {
  background-color: white;
  width: 7.5rem;
  height: 0.88rem;
  line-height: 0.89rem;
  padding: 0.05rem 0.3rem;
  position: relative;
  z-index: 1;
}

.replace-title-input {
  width: 6.9rem;
  height: 0.88rem;
  border: none;
  border-bottom: 1px solid #e6e6e6;
  padding: 0;
  color: #333333;
  font-size: 0.28rem;
  font-family: 'PingFangSC Regular';
}

::-webkit-input-placeholder {
  color: #999999;
  font-size: 0.28rem;
  font-family: 'PingFangSC Regular';
}

.publish-bottom {
  background-color: white;
  margin-top: 0.2rem;
}

.is-link-svg {
  padding-top: 0.05rem;
  display: inline-block;
  color: #999999;
  font-size: 0.3rem;
}

.my-cell {
  line-height: 0.38rem;
  display: flex;
  justify-content: space-between;
  padding: 0 0.3rem;
  align-items: center;
  height: 0.88rem;
  border-bottom: 1px solid #e6e6e6;
}

.my-cell:last-child {
  margin-bottom: 0.88rem;
}

.leftmoneysvg {
  color: #333333;
  font-size: 0.42rem;
  margin-right: 0.16rem;
}

.placediv {
  height: 0.9rem;
  text-align: center;
  line-height: 0.9rem;
  font-size: 0.3rem;
  color: #333333;
  font-family: 'PingFangSC Regular';
}

.my-money-input {
  width: 1.6rem;
  padding-left: 0.3rem;
  text-align: left;
  border: none;
  font-family: 'PingFangSC Regular';
  font-size: 0.3rem;
  color: #999999;
}

.my-money-input::-webkit-input-placeholder {
  color: #999999;
  font-size: 0.3rem;
  font-family: 'PingFangSC Regular';
}

.loadmore-ul {
  padding-bottom: 0.2rem;
  list-style: none;
}

.facedismoney {
  font-size: 0.3rem;
  margin-left: 0.1rem;
}

.goutyle {
  font-size: 0.34rem;
  line-height: 0.7rem;
}

.gouselected {
  color: #fb685b;
}

.nodata {
  display: none;
}
</style>